package org.vaadin.nf4v.ext.runtime.menu.menubar;

import com.vaadin.ui.Component;
import com.vaadin.ui.MenuBar;
import org.vaadin.nf4v.core.NavigableApplication;
import org.vaadin.nf4v.core.NavigationException;
import org.vaadin.nf4v.core.config.ContextConfig;
import org.vaadin.nf4v.core.config.PageConfig;

/**
 * Utility which provides basic menu building functionality (according to @Menu annotations).
 * It just redirect calls to default menu builder implementation.
 * <p/>
 * If default menu building logic should be extended, extend MenuBuilderImpl and use it directly
 * or make utility class like that.
 */
public class MenuBuilder {
    private static final MenuBuilderImpl builder = new MenuBuilderImpl();

    /**
     * Builds entire application menu (including all contexts).
     * in other way automatic context switching will not work (framework will try to find page in current context).
     * Note: includeContextNodes affects only if window classes were also annotated with @Menu annotation
     *
     * @param includeContextNodes true to include context nodes in menu building
     * @return menu bar
     */
    public static MenuBar buildFullMenuTree(boolean includeContextNodes) {
        return builder.buildFullMenuTree(NavigableApplication.getCurrent().getNavigationConfig(), includeContextNodes);
    }

    /**
     * Builds menu tree for context.
     * Note: includeContextNode affects only if window class were also annotated with @Menu annotation
     *
     * @param context            context name
     * @param includeContextNode true to include context node in menu building
     * @return menu bar
     * @throws NavigationException if context with provided name is not found
     */
    public static MenuBar buildContextMenuTree(String context, boolean includeContextNode) throws NavigationException {
        ContextConfig config = NavigableApplication.getCurrent().getNavigationConfig().getContextConfig(context);
        return builder.buildContextMenuTree(config, includeContextNode);
    }

    /**
     * Builds menu tree for current page
     *
     * @param context           context name
     * @param pageClass         page class
     * @param includingThisPage true to include page itself as root menu item
     * @return menu bar
     * @throws NavigationException if context or page not found
     */
    public static MenuBar buildPageMenuTree(String context, Class<? extends Component> pageClass, boolean includingThisPage)
            throws NavigationException {
        PageConfig config = NavigableApplication.getCurrent().getNavigationConfig().getPageConfig(context, pageClass);
        return builder.buildPageMenuTree(config, includingThisPage);
    }
}
